/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.orderManage;

import java.util.List;

import com.javaeye.chenweihuacwh.dto.orderManage.CustomerOrder;
import com.javaeye.chenweihuacwh.dto.orderManage.OrderDetail;
import com.javaeye.chenweihuacwh.service.orderManage.IOrderService;
import com.javaeye.chenweihuacwh.util.ListItem;
import com.javaeye.chenweihuacwh.util.ListUtil;
import com.javaeye.chenweihuacwh.web.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

public class OrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private OrderCondition condition;
	
	private PageInfo pageInfo;
	
	private List<CustomerOrder> orderList;
	
	private String orderId;
	
	/**
	 * 出货单信息
	 */
	private CustomerOrder order;
	
	/**
	 * 出货单明细产品ID
	 */
	private List<Integer> productIds;
	
	/**
	 * 出货单明细产品名称
	 */
	private List<String> productNames;
	
	/**
	 * 出货单明细产品分类
	 */
	private List<String> productCategorys;
	
	/**
	 * 出货单明细产品型号
	 */
	private List<String> productTypes;
	
	/**
	 * 出货单明细产品代码
	 */
	private List<String> productNos;
	
	/**
	 * 出货单明细客户代码
	 */
	private List<String> customNos;
	
	/**
	 * 出货单明细产品数量
	 */
	private List<Integer> productNums;
	
	/**
	 * 订单明细产品单位
	 */
	private List<String> productUnits;
	
	/**
	 * 订单明细产品价格
	 */
	private List<Double> productPrices;
	
	private IOrderService service;
	
	private List<ListItem> orderStatus;
	
	public OrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new OrderCondition();
		condition.setCreateDateType(OrderCondition.DATE_TYPE_CURRENT_MONTH);
		condition.setCustomName("");
		condition.setProductName("");
	}
	
	/**
	 * 查询出货单基本信息列表
	 * @return
	 * @throws Exception
	 */
	public String queryOrderList() throws Exception {
		orderStatus =  ListUtil.orderStatusList();
		orderList = service.getOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询出货单基本信息
	 * @return
	 * @throws Exception
	 */
	public String queryOrderBaseInfo() throws Exception {
		order = service.getOrderBaseInfo(orderId);
		if (order.getLogisticsType() != null) {
			order.setLogisticsType(ListUtil.logisticsTypeName(order.getLogisticsType()));
		}
		return SUCCESS;
	}
	
	/**
	 * 关闭出货单
	 * @return
	 * @throws Exception
	 */
	public String closeOrder() throws Exception {
		service.closeOrder(orderId);
		return SUCCESS;
	}
	
	/**
	 * 申请关闭出货单
	 * @return
	 * @throws Exception
	 */
	public String saveApplyClose() throws Exception {
		service.saveApplyClose(orderId, order);
		return SUCCESS;
	}
	
	/**
	 * 取消关闭出货单
	 * @return
	 * @throws Exception
	 */
	public String cancelCloseOrder() throws Exception {
		service.cancelCloseOrder(orderId);
		return SUCCESS;
	}
	
	/**
	 * 同意关闭出货单
	 * @return
	 * @throws Exception
	 */
	public String closeApplyOrder() throws Exception {
		service.closeApplyOrder(orderId);
		return SUCCESS;
	}
	
	/**
	 * 保存出货单
	 * @return
	 * @throws Exception
	 */
	public String saveOrder() throws Exception {
		for (int i = 0; i < productNames.size(); i++) {
			OrderDetail detail = new OrderDetail();
			detail.setProductId(productIds.get(i));
			detail.setProductName(productNames.get(i));
			detail.setProductCategory(productCategorys.get(i));
			detail.setProductType(productTypes.get(i));
			detail.setProductNo(productNos.get(i));
			detail.setCustomNo(customNos.get(i));
			detail.setNumber(productNums.get(i));
			detail.setUnit(productUnits.get(i));
			detail.setPrice(productPrices.get(i));
			order.addDetail(detail);
		}
		service.saveOrder(order);
		orderId = order.getId();
		order = service.getOrderBaseInfo(orderId);
		return SUCCESS;
	}

	public CustomerOrder getOrder() {
		return order;
	}

	public void setOrder(CustomerOrder order) {
		this.order = order;
	}

	public List<Integer> getProductIds() {
		return productIds;
	}

	public void setProductIds(List<Integer> productIds) {
		this.productIds = productIds;
	}

	public List<String> getProductNames() {
		return productNames;
	}

	public void setProductNames(List<String> productNames) {
		this.productNames = productNames;
	}

	public List<Integer> getProductNums() {
		return productNums;
	}

	public void setProductNums(List<Integer> productNums) {
		this.productNums = productNums;
	}

	public void setService(IOrderService service) {
		this.service = service;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getOrderId() {
		return orderId;
	}
	
	public OrderCondition getCondition() {
		return condition;
	}
	
	public void setCondition(OrderCondition condition) {
		this.condition = condition;
	}
	
	public PageInfo getPageInfo() {
		return pageInfo;
	}
	
	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}
	
	public List<CustomerOrder> getOrderList() {
		return orderList;
	}
	
	public void getOrderList(List<CustomerOrder> orderList) {
		this.orderList = orderList;
	}

	public IOrderService getService() {
		return service;
	}

	public List<ListItem> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<ListItem> orderStatus) {
		this.orderStatus = orderStatus;
	}
	
	public void setProductTypes(List<String> productTypes) {
		this.productTypes = productTypes;
	}

	public List<String> getProductTypes() {
		return productTypes;
	}

	public void setCustomNos(List<String> customNos) {
		this.customNos = customNos;
	}

	public List<String> getCustomNos() {
		return customNos;
	}

	public void setProductUnits(List<String> productUnits) {
		this.productUnits = productUnits;
	}

	public List<String> getProductUnits() {
		return productUnits;
	}

	/**
	 * @param productCategorys the productCategorys to set
	 */
	public void setProductCategorys(List<String> productCategorys) {
		this.productCategorys = productCategorys;
	}

	/**
	 * @return the productCategorys
	 */
	public List<String> getProductCategorys() {
		return productCategorys;
	}

	/**
	 * @param productNos the productNos to set
	 */
	public void setProductNos(List<String> productNos) {
		this.productNos = productNos;
	}

	/**
	 * @return the productNos
	 */
	public List<String> getProductNos() {
		return productNos;
	}
	
	public void setProductPrices(List<Double> productPrices) {
		this.productPrices = productPrices;
	}
	
	public List<Double> getProductPrices() {
		return productPrices;
	}	
}
